<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <title>Fluenty Survey</title>
    <style>
      body {
        margin: 0 auto;
        padding: 0;
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans',
          'Helvetica Neue', sans-serif;
        background-color: #fff;
        max-width: 600px;
      }
      .container {
        padding: 20px;
      }
      .nav-progress-container {
        display: flex;
        align-items: center;
        margin-bottom: 30px;
        gap: 15px;
      }
      .back-button {
        width: 30px;
        height: 30px;
        border: none;
        background: none;
        cursor: pointer;
        color: #2b85e4;
        font-size: 20px;
        padding: 0;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .back-button svg {
        width: 20px;
        height: 20px;
        fill: currentColor;
      }
      .progress-bar {
        flex: 1;
        height: 6px;
        background-color: #e0e0e0;
        border-radius: 3px;
      }
      .progress {
        height: 100%;
        width: 73.26%;
        background-color: #2b85e4;
        border-radius: 3px;
      }
      .title {
        font-size: 36px;
        font-weight: bold;
        color: #0B4983;
        margin-bottom: 15px;
        line-height: 1.3;
        text-align: center;
      }
      .subtitle {
        font-size: 22px;
        font-weight: 600;
        color: #333;
        margin-bottom: 25px;
        text-align: center;
      }
      .words-container {
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-start;
        gap: 12px;
        margin-bottom: 30px;
      }
      .word-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 10px 16px;
        border: 1px solid #e0e0e0;
        border-radius: 50px;
        background-color: white;
        cursor: pointer;
        flex-grow: 0;
      }
      .word-item.selected {
        background-color: #e6f4ff;
        border-color: #2b85e4;
      }
      .word-item:hover {
        border-color: #2b85e4;
      }
      .word-text {
        font-size: 18px;
        font-weight: 500;
        color: #333;
      }
      .word-checkbox {
        width: 22px;
        height: 22px;
        border: 2px solid #e0e0e0;
        border-radius: 50%;
        flex-shrink: 0;
        margin-left: 10px;
      }
      .word-item.selected .word-checkbox {
        border-color: #2b85e4;
        position: relative;
      }
      .word-item.selected .word-checkbox::after {
        content: '';
        position: absolute;
        width: 12px;
        height: 12px;
        background-color: #2b85e4;
        border-radius: 50%;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
      .continue-button {
        background-color: #3b82f6;
        color: white;
        border: none;
        border-radius: 12px;
        padding: 16px;
        font-size: 18px;
        font-weight: bold;
        width: 100%;
        cursor: pointer;
        text-align: center;
        margin-top: 20px;
      }
      .continue-button:hover {
        background-color: #2563eb;
      }
      @media (max-width: 480px) {
        .word-item {
          min-width: calc(50% - 12px);
        }
        .title {
          font-size: 32px;
        }
        .subtitle {
          font-size: 20px;
        }
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="nav-progress-container">
        <button class="back-button">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
            <path d="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z" />
          </svg>
        </button>
        <div class="progress-bar"><div class="progress"></div></div>
      </div>
      <h1 class="title">Select all the words you know:</h1>
      <h2 class="subtitle">A1-A2 Beginner Level</h2>
      <div class="words-container">
        <div class="word-item" style="min-width: 60px">
          <span class="word-text">cat</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 70px">
          <span class="word-text">jump</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 80px">
          <span class="word-text">table</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 80px">
          <span class="word-text">laugh</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 60px">
          <span class="word-text">run</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 80px">
          <span class="word-text">drink</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 60px">
          <span class="word-text">sky</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 90px">
          <span class="word-text">flower</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 60px">
          <span class="word-text">eat</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 90px">
          <span class="word-text">listen</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 70px">
          <span class="word-text">door</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 70px">
          <span class="word-text">book</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 80px">
          <span class="word-text">chair</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 80px">
          <span class="word-text">happy</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 70px">
          <span class="word-text">walk</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 80px">
          <span class="word-text">sleep</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 90px">
          <span class="word-text">friend</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 90px">
          <span class="word-text">family</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 60px">
          <span class="word-text">car</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 70px">
          <span class="word-text">play</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 60px">
          <span class="word-text">sun</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 60px">
          <span class="word-text">dog</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 60px">
          <span class="word-text">fish</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 80px">
          <span class="word-text">dance</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 70px">
          <span class="word-text">cook</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 80px">
          <span class="word-text">water</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 60px">
          <span class="word-text">red</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 70px">
          <span class="word-text">blue</span>
          <div class="word-checkbox"></div>
        </div>
        <div class="word-item" style="min-width: 100px">
          <span class="word-text">morning</span>
          <div class="word-checkbox"></div>
        </div>
      </div>
      <button class="continue-button">Continue</button>
    </div>
    <script>
      document.addEventListener('DOMContentLoaded', function () {
        const wordItems = document.querySelectorAll('.word-item');
        const backButton = document.querySelector('.back-button');
        const continueButton = document.querySelector('.continue-button');
        let selectedWords = [];
    
        // 获取当前页面名称
        const pageName = window.location.pathname.split('/').pop().split('.')[0];
        // 从 localStorage 中读取之前保存的选中单词信息
        const savedData = localStorage.getItem(pageName);
        if (savedData) {
          const parsedData = JSON.parse(savedData);
          selectedWords = parsedData.words;
          // 遍历单词项，将之前选中的设置为选中状态
          wordItems.forEach(item => {
            const wordText = item.querySelector('.word-text').textContent;
            if (selectedWords.includes(wordText)) {
              item.classList.add('selected');
            }
          });
        }

        wordItems.forEach(item => {
          item.addEventListener('click', function () {
            this.classList.toggle('selected');
            const wordText = this.querySelector('.word-text').textContent;
            if (this.classList.contains('selected')) {
              if (!selectedWords.includes(wordText)) {
                selectedWords.push(wordText);
              }
            } else {
              const index = selectedWords.indexOf(wordText);
              if (index > -1) {
                selectedWords.splice(index, 1);
              }
            }
          });
        });

        backButton.addEventListener('click', function () {
          window.location.href = 'helpYou.html';
        });

        continueButton.addEventListener('click', function () {
          if (selectedWords.length > 0) {
            // 获取页面标题和当前页面名称
            const pageTitle = document.querySelector('.page-title')
              ? document.querySelector('.page-title').textContent.trim()
              : 'Select all the words you know';

            // 创建数据对象
            const data = {
              title: pageTitle,
              value: selectedWords.join(', '), // 用逗号分隔多个选择
              words: selectedWords, // 保存为数组，便于后续处理
              count: selectedWords.length, // 选择的单词数量
            };

            // 以 JSON 格式存储到 localStorage
            localStorage.setItem(pageName, JSON.stringify(data));

            window.location.href = 'selectAllB.html';
          } else {
            alert('Please select at least one word before continuing');
          }
        });
      });
    </script>
  </body>
</html>
